package com.zr.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.zr.dao.OrderDao;
import com.zr.datamodel.GoodTypeDM;
import com.zr.datamodel.OrderDM;
import com.zr.datamodel.PointPriceDM;

import net.sf.json.JSONArray;

/**
 * 
 * @author qiuchun
 *
 */
@Repository("orderDao")
public class OrderDaoImpl extends BaseDaOImpl<OrderDM> implements OrderDao{

	@Override
	public long findOrderCount(){
		// TODO Auto-generated method stub
		String hql="select count(*) from OrderDM";
		List<Object> obj=this.getCurrentSession().createQuery(hql).list();
		return (long) obj.get(0);
	}
	@Override
	public List<OrderDM> findOrderMsg(int page, int rows,String senderName) throws Exception{
		// TODO Auto-generated method stub
		String hql="from OrderDM o";
		String whereHql=hql;
			if(senderName==null){
				whereHql=hql;
			}else if(null!=senderName){
				whereHql=hql + " where o.senderName  like '%"+senderName+"%'";
			}
			Query query=this.getCurrentSession().createQuery(whereHql);
			query.setFirstResult((page-1)*rows);//设置起始页
			query.setMaxResults(rows); //记录条数
			List<OrderDM>  orders=query.list();
		return orders;
	}

	@Override
	public void delOrderMsg(int id) throws Exception{
		// TODO Auto-generated method stub
		OrderDM order = (OrderDM) this.getCurrentSession().get(OrderDM.class, id);
		this.getCurrentSession().delete(order);
	}

	@Override
	public List<OrderDM> insertOrderMsg(OrderDM orders) throws Exception{
		// TODO Auto-generated method stub
		List<OrderDM> order=(List<OrderDM>) this.getCurrentSession().save(orders);
		return order;
	}
	@Override
	public JSONArray findGoodType() {
		// TODO Auto-generated method stub
		Map<String, String> map= new HashMap<String, String>();
		JSONArray jso= new JSONArray();
		String hql="select new GoodTypeDM (id,name) from GoodTypeDM";
		try{
		Query query=this.getCurrentSession().createQuery(hql);
		List<GoodTypeDM> list=query.list();
		for (GoodTypeDM goodType : list) {
			String name= goodType.getName();
			String id=String.valueOf(goodType.getId());
			map.put("id", id);
			map.put("name", name);
			jso.add(map);
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		return jso;
	}
	@Override
	public void updateOrder(OrderDM orderMsg) {
		// TODO Auto-generated method stub
		OrderDM order=(OrderDM)this.getCurrentSession().get(OrderDM.class, orderMsg.getId());
		order.setNumber(orderMsg.getNumber());
		order.setOrderType(orderMsg.getOrderType());
		order.setPayMoney(orderMsg.getPayMoney());
		order.setPayWay(orderMsg.getPayWay());
		order.setReceiverAdress(orderMsg.getReceiverAdress());
		order.setReceiverName(orderMsg.getReceiverName());
		order.setReceiverPhone(orderMsg.getReceiverPhone());
		order.setSendData(orderMsg.getSendData());
		order.setSenderAdress(orderMsg.getSenderAdress());
		order.setSenderName(orderMsg.getSenderName());
		order.setSenderPhone(orderMsg.getSenderPhone());
		order.setStatus(orderMsg.getStatus());
		order.setConutMoney(orderMsg.getConutMoney());
		order.setEndPlace(orderMsg.getEndPlace());
		order.setStartPlace(orderMsg.getStartPlace());
		order.setGoods(orderMsg.getGoods());
		this.getCurrentSession().update(order);
	}

	
}
